
// pages/new_inter/hotel_fabiao/hotel_fabiao.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploaderList: [], //已选择的图片临时路径数组
    uploaderNum: 0, //已选择图片个数
    showUpload: true //用来判断是否可继续选择图片,当传至9张时不可继续上传
  },
  // 删除图片
  clearImg: function (e) {
    var nowList = []; //新数据
    var uploaderList = this.data.uploaderList; //原数据

    for (let i = 0; i < uploaderList.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        continue;
      } else {
        nowList.push(uploaderList[i])
      }
    }
    this.setData({
      uploaderNum: this.data.uploaderNum - 1,
      uploaderList: nowList,
      showUpload: true
    })
  },
  //展示图片
  showImg: function (e) {
    var that = this;
    wx.previewImage({
      urls: that.data.uploaderList,
      current: that.data.uploaderList[e.currentTarget.dataset.index]
    })
  },
  //上传图片
  upload: function (e) {
    var that = this;
    wx.chooseImage({
      // count: 9 - that.data.uploaderNum, // 默认9
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        let uploaderList = that.data.uploaderList.concat(tempFilePaths);
        if (uploaderList.length == 9) {
          that.setData({
            showUpload: false
          })
        }
        that.setData({
          uploaderList: uploaderList,
          uploaderNum: uploaderList.length,
        })
        for (var i = 0; i < tempFilePaths.length; i++) {
          // console.log(tempFilePaths[i]);
          that.upload_server(tempFilePaths[i]);
          // file.push(res.tempFilePaths[i]);
        }

      }
    })
  },
  //上传服务器
  upload_server: function (data) {
    var that = this;
    that.setData({
      file: [],
    })
    wx.uploadFile({
      url: app.globalData.apiUrl + 'Shop/upimg', // 仅为示例，非真实的接口地址
      filePath: data,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        if (res.data !== "") {
          // var back_push=that.data.file;
          // console.log()
          that.data.file.push(res.data)
          // that.setData({
          //   back_push: back_push,
          // })
        }
        console.log(that.data.file, '返回图片长度')
      }
    })
  },
  //提交入驻信息
  formSubmit: function (e) {

    var that = this;
    // console.log(that.data.file.length, '返回图片长度')
    // return;
    // if (that.data.isyuedu !== "yes") {
    //   wx.showToast({
    //     title: '请勾选协议',
    //     duration: 2000
    //   })
    //   return;
    // }
    // if (that.data.touxiang == "../images/jiaico.png") {
    //   wx.showToast({
    //     title: '请上传头像',
    //     duration: 2000
    //   })
    //   return;
    // }
    // if (that.data.file.length !== that.data.touxiang.length) {
    //   wx.showToast({
    //     title: '请等待图片上传完成',
    //     duration: 2000
    //   })
    //   return;
    // }
    var formData = e.detail.value;
    // var a = [1, 2, 3, 4]
    formData['image'] = that.data.file;
    formData['userid'] = wx.getStorageSync('userId');
    formData['type'] = 2;
    console.log(formData);
    // return ;         
    wx.request({
      url: app.globalData.apiUrl + 'Housecomment/save',
      data: formData,
      method: 'post',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.status == 1) {


          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration: 2000,
            success: function () {
              wx.navigateBack({
                delta: 1
              })
              // setTimeout(function () {
              //   wx.switchTab({
              //     url: '../personal/personal',
              //   })
              // }, 2000);
            }
          })
         
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000
          })
        }
        // that.shenqing_state();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  //onShareAppMessage: function () {

  //},
  index: function () {
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },
})

